# Maintainer: Jaroslav Lichtblau <svetlemodry@archlinux.org>
# Contributor: Vesa Kaihlavirta <vegai@iki.fi>

pkgbase=quassel
pkgname=('quassel-core'
         'quassel-client'
         'quassel-client-qt'
         'quassel-monolithic'
         'quassel-monolithic-qt'
         'quassel-common')
pkgver=0.14.0
pkgrel=2
pkgdesc="Next-generation distributed IRC client"
arch=('loong64' 'x86_64')
url="https://quassel-irc.org/"
license=('GPL')
makedepends=('qt5-base' 'qt5-tools' 'qt5-webengine' 'qca'
             'qt5-multimedia' 'knotifyconfig' 'sonnet' 'libldap' 'cmake'
             'extra-cmake-modules' 'python' 'hicolor-icon-theme' 'boost')
source=(https://quassel-irc.org/pub/$pkgbase-$pkgver.tar.bz2
        $pkgbase.service
        $pkgbase.sysusers
        $pkgbase.tmpfiles
        $pkgbase.conf)
sha256sums=('bbd46ca429c1518480904c9e2b0ab3a0412fe197f3ce4879974e06ed3a0093a3'
            '5dbe20290f3361b9b7a74a52905137e76b656976febf2d31082a2276f9dcde7f'
            '3c72441a99e2668c6a8a751fa07beeb44f937576c8a1b5f615e4a55f841d93d9'
            '2afd4340c7713f6533e5d175a86b28fd118ecd907776c2b10925d1a4fb31cdca'
            'f3031ea8217e01ba42cea14606169e3e27affa5918968ffd5a03c21ae92fe2b8')

_build() (
  cmake -H$pkgbase-$pkgver -Bbuild-$1 \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=None \
    -DENABLE_SHARED=OFF \
    -DCMAKE_CXX_FLAGS="$CXXFLAGS -Wno-suggest-override" \
    "${@:2}" \
    -Wno-dev
  cmake --build build-$1
)

_install() {
  DESTDIR="${pkgdir}" cmake --build build-$1 --target install
}

build() {
  _build core \
    -DWANT_QTCLIENT=OFF \
    -DWANT_MONO=OFF \
    -DWITH_BUNDLED_ICONS=OFF \
    -DWITH_WEBENGINE=OFF

  _build monolithic \
    -DWANT_CORE=OFF \
    -DWANT_QTCLIENT=OFF \
    -DWITH_KDE=ON

  _build monolithic-qt \
    -DWANT_CORE=OFF \
    -DWANT_QTCLIENT=OFF \
    -DWITH_WEBENGINE=OFF \
    -DCMAKE_DISABLE_FIND_PACKAGE_dbusmenu-qt5=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Qt5Multimedia=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Phonon4Qt5=ON

  _build client \
    -DWANT_CORE=OFF \
    -DWANT_MONO=OFF \
    -DWITH_KDE=ON

  _build client-qt \
    -DWANT_CORE=OFF \
    -DWANT_MONO=OFF \
    -DWITH_WEBENGINE=OFF \
    -DCMAKE_DISABLE_FIND_PACKAGE_dbusmenu-qt5=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Qt5Multimedia=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Phonon4Qt5=ON
}

package_quassel-core() {
pkgdesc="Next-generation distributed IRC client - core only"
depends=('icu' 'qca' 'libldap')
optdepends=('postgresql: PostgreSQL database support')
backup=(etc/conf.d/quassel)

  _install core

  # Remove unneeded pixmap icons
  rm -r "${pkgdir}"/usr/share

  install -Dm644 $pkgbase.sysusers \
    "${pkgdir}"/usr/lib/sysusers.d/$pkgbase.conf
  install -Dm644 $pkgbase.tmpfiles \
    "${pkgdir}"/usr/lib/tmpfiles.d/$pkgbase.conf
  install -Dm644 $pkgbase.service \
    "${pkgdir}"/usr/lib/systemd/system/$pkgbase.service
  install -Dm644 $pkgbase.conf \
    "${pkgdir}"/etc/conf.d/$pkgbase
}

package_quassel-client() {
pkgdesc="Next-generation distributed IRC client - client only, KDE version"
depends=('quassel-common' 'qt5-base' 'qt5-webengine' 'qt5-multimedia' 'knotifyconfig')
optdepends=('perl: for builtin /exec scripts')
conflicts=('quassel-client-qt')

  _install client
  install -Dm644 $pkgbase-$pkgver/data/quasselclient.appdata.xml "$pkgdir/usr/share/metainfo/quasselclient.appdata.xml"

  # Split quassel-common
  rm -r "$pkgdir"/usr/share/{icons,knotifications5,quassel}
}

package_quassel-client-qt() {
pkgdesc="Next-generation distributed IRC client - client only, Qt version with reduced deps"
depends=('quassel-common' 'qt5-base' 'qt5-svg' 'sonnet')
optdepends=('perl: for builtin /exec scripts')
conflicts=('quassel-client')
replaces=('quassel-client-small' 'quassel-remote')

  _install client-qt

  # Split quassel-common
  rm -r "$pkgdir"/usr/share/{icons,quassel}

  # Ignore package by AppStream to avoid duplicated IDs
  echo 'X-AppStream-Ignore=true' >> "$pkgdir/usr/share/applications/quasselclient.desktop"
}

package_quassel-monolithic() {
pkgdesc="Next-generation distributed IRC client - monolithic, KDE version"
depends=('quassel-common' 'qt5-base' 'qt5-webengine' 'qt5-multimedia' 'qca'
         'knotifyconfig' 'libldap')
optdepends=('perl: for builtin /exec scripts'
            'postgresql: PostgreSQL database support')
conflicts=('quassel-monolithic-qt')

  _install monolithic
  install -Dm644 $pkgbase-$pkgver/data/quassel.appdata.xml "$pkgdir/usr/share/metainfo/quassel.appdata.xml"

  # Split quassel-common
  mkdir "$srcdir/quassel-common"
  mv "$pkgdir"/usr/share/{icons,knotifications5,quassel} \
     "$srcdir/quassel-common/"
}

package_quassel-monolithic-qt() {
pkgdesc="Next-generation distributed IRC client - monolithic, Qt version with reduced deps"
depends=('quassel-common' 'qca' 'qt5-base' 'qt5-svg' 'sonnet')
optdepends=('perl: for builtin /exec scripts'
            'postgresql: PostgreSQL database support')
conflicts=('quassel-monolithic')

  _install monolithic-qt

  # Split quassel-common
  rm -r "$pkgdir"/usr/share/{icons,quassel}

  # Ignore package by AppStream to avoid duplicated IDs
  echo 'X-AppStream-Ignore=true' >> "$pkgdir/usr/share/applications/quassel.desktop"
}

package_quassel-common() {
  pkgdesc="Common files for Quassel"
  depends=('hicolor-icon-theme')

  install -dm755 "$pkgdir/usr/share"
  mv quassel-common/* "$pkgdir/usr/share"
}
